package com.webster.isw.core.service.impl;

import com.webster.isw.core.gen.entity.*;
import com.webster.isw.core.gen.mapper.ScRequestLogMapper;
import com.webster.isw.core.gen.mapper.ScScheduledLogMapper;
import com.webster.isw.core.gen.mapper.ScSessionHistoryMapper;
import com.webster.isw.core.gen.mapper.ScSessionMapper;
import com.webster.isw.core.service.AppLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.UUID;

/**
 * 程序的日志服务
 * Created by Webster on 16/5/7.
 */
@Service("appLogService")
public class AppLogServiceImpl extends BaseServiceImpl implements AppLogService{


    /**
     * Field log: 日志
     */
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * Field scScheduledLogMapper: 计划任务日志服务
     */
    @Autowired
    private ScScheduledLogMapper scScheduledLogMapper;

    /**
     * Field tosm: 在线session服务
     */
    @Autowired
    private ScSessionMapper scSessionMapper;

    /**
     * Field toshm: 历史session服务
     */
    @Autowired
    private ScSessionHistoryMapper scSessionHistoryMapper;

    /**
     * Field trrlm: 请求日志服务
     */
    @Autowired
    private ScRequestLogMapper scRequestLogMapper;

    /**
     * 存储请求响应日志-该方法吃掉所有异常,不影响其他的应用-无事务-超时时间2秒
     *
     * @param scRequestLog 日志
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, timeout = 2)
    public void saveRequestLog(ScRequestLog scRequestLog) {
        try {
            scRequestLogMapper.insertSelective(scRequestLog);
        } catch (DataAccessException e) {
            logger.error("saveRequestLog:", e);
        }
    }

    /**
     * 清空OnlineSession表中的数据-该方法吃掉所有异常,不影响其他的应用-无事务-超时时间2秒
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, timeout = 2)
    public void clearSessionDb() {
        try {
            scSessionMapper.deleteByExample(new ScSessionCriteria());
        }catch (DataAccessException e){
            logger.error("clearSessionDb:",e);
        }
    }

    /**
     * session创建记录-该方法吃掉所有异常,不影响其他的应用-无事务-超时时间2秒
     *
     * @param scSession 在线session
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, timeout = 2)
    public void sessionCreated(ScSession scSession) {
        try {
            scSessionMapper.insertSelective(scSession);
        }catch (DataAccessException e){
            logger.error("sessionCreated:",e);
        }
    }

    /**
     * session销毁记录-该方法吃掉所有异常,不影响其他的应用-无事务-超时时间2秒
     *
     * @param scSessionHistory session历史
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, timeout = 2)
    public void sessionDestroyed(ScSessionHistory scSessionHistory) {
        try {
            scSessionMapper.deleteByPrimaryKey(scSessionHistory.getId());
            scSessionHistoryMapper.insertSelective(scSessionHistory);
        }catch (DataAccessException e){
            logger.error("sessionDestroyed:",e);
        }
    }

    /**
     * Description: 保存记录(此方法吃掉所有异常,避免异常日志切换形成递归.)
     *
     * @param msg 信息
     */
    @Override
    public void saveScheduledLog(String msg) {
        try {
            String id = UUID.randomUUID().toString();
            ScScheduledLog scScheduledLog = new ScScheduledLog();
            scScheduledLog.setId(id);
            scScheduledLog.setCreateAt(new Date());
            scScheduledLog.setContent(msg);

            scScheduledLogMapper.insert(scScheduledLog);
        }catch (DataAccessException e){
            logger.error("saveScheduledLog保存日志异常:",e);
        }
    }

}
